package it.globalgamejam.scar.game.world 
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import it.globalgamejam.scar.Assets;
	import it.globalgamejam.scar.game.GameWorld;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.graphics.Stamp;
	import net.flashpunk.Sfx;
	import net.flashpunk.tweens.misc.MultiVarTween;
	import net.flashpunk.tweens.misc.VarTween;
	import net.flashpunk.utils.Draw;
	/**
	 * ...
	 * @author N1xx1
	 */
	public class Ball extends Entity
	{
		
		public var isCatapulted:Boolean = false;
		
		public var speedX:Number = 0;
		public var speedY:Number = 0;
		
		private var moving:Boolean = false;
		private var movingTimer:Number = 0;
		
		private var spritemap:Spritemap = new Spritemap(Assets.MEDDA, 103, 103);
		
		public function Ball(x:Number, y:Number)
		{
			super(x + 10, y + 10);
			
			spritemap.add("idle", [0], 20, true);
			spritemap.add("move", [0, 1, 2], 10, true);
			spritemap.originX = 51;
			spritemap.originY = 51;
			spritemap.x = 51;
			spritemap.y = 51;
			graphic = spritemap;
			
			name = "ball";
			
			setHitbox(100, 100);
			layer = 40;
		}
		
		public function launch(dir:int):void {
			isCatapulted = true;
			var tween:VarTween = new VarTween(function ():void {
				isCatapulted = false;
			});
			if (dir == 0)
				tween.tween(this, "y", y - 240, 1);
			else if (dir == 2)
				tween.tween(this, "y", y + 240, 1);
			else if (dir == 1)
				tween.tween(this, "x", x + 240, 1);
			else if (dir == 3)
				tween.tween(this, "x", x - 240, 1);
			FP.world.addTween(tween);
			
			trace("catapulted");
		}
		
		public override function update():void 
		{
			super.update();
			
			if (GameWorld(FP.world).isPlaying && !isCatapulted) {
				var goal:Goal = FP.world.getInstance("goal") as Goal;
				if (goal) {
					if (collideWith(goal, x, y)) {
						GameWorld(FP.world).nextLevel();
					}
				}
				
				var player:Player = FP.world.getInstance("player") as Player;
				if (player) {
					if (collideWith(player, x, y)) {
						player.deltaSpeed = 10;
						if (player.speedY < -100) player.speedY = -100;
						if (player.speedY >  100) player.speedY = 100;
						if (player.speedX < -100) player.speedX = -100;
						if (player.speedX >  100) player.speedX = 100;
						
						var dx:Number = player.x + player.width/2 - (x + width/2);
						var dy:Number = player.y + player.height/2 - (y + height/2);
						var d:Number = Math.max(Math.abs(dx), Math.abs(dy));
						
						if (d >= 1) {
							d = Math.sqrt(d);
							dx /= d;
							dy /= d;
							
							var d1:Number = 1 / d;
							if (d1 > 1) d1 = 1;
							
							dx *= d1;
							dy *= d1;
							dx *= 34;
							dy *= 34;
							
							if (Math.abs(dx) > Math.abs(dy)) {
								speedX += -dx;
								player.speedX += dx;
							} else {
								speedY += -dy;
								player.speedY += dy;
							}
						}
					} else {
						player.deltaSpeed = 20;
					}
				}
				
				speedX *= 0.9;
				speedY *= 0.9;
				
				var speedXabs:Number = Math.abs(speedX);
				var speedYabs:Number = Math.abs(speedY);
				
				if (speedXabs < 4) speedX = 0;
				if (speedYabs < 4) speedY = 0;
				
				if (speedXabs < speedYabs) {
					spritemap.angle = 0;
				} else if (speedXabs != 0 || speedYabs != 0) {
					spritemap.angle = 90;
				}
				
				if (speedXabs > 3 || speedYabs > 3) {
					if (!moving) {
						spritemap.play("move");
						moving = true;
						GameWorld(FP.world).sfxRoll1.loop();
					}
					movingTimer = 0;
				} else if(movingTimer > 0.5 && moving) {
					spritemap.play("idle");
					GameWorld(FP.world).sfxRoll1.stop();
					moving = false;
				} else if(moving) {
					movingTimer += FP.elapsed;
				}
				moveBy(speedX * FP.elapsed, speedY * FP.elapsed, "wall");
			}
		}
		
		override public function moveCollideX(e:Entity):Boolean
		{
			speedX = 0;
			return true;
		}
		
		override public function moveCollideY(e:Entity):Boolean
		{
			speedY = 0;
			return true;
		}
		
	}

}